package cn.iocoder.yudao.module.mqtt.service.configalarm;

import cn.hutool.core.collection.CollUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.mqtt.controller.admin.configalarm.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.configalarm.ConfigAlarmDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mqtt.dal.mysql.configalarm.ConfigAlarmMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.mqtt.enums.ErrorCodeConstants.*;

/**
 * MQTT报警配置 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ConfigAlarmServiceImpl implements ConfigAlarmService {

    @Resource
    private ConfigAlarmMapper configAlarmMapper;

    @Override
    public Long createConfigAlarm(ConfigAlarmSaveReqVO createReqVO) {
        // 插入
        ConfigAlarmDO configAlarm = BeanUtils.toBean(createReqVO, ConfigAlarmDO.class);
        configAlarmMapper.insert(configAlarm);
        // 返回
        return configAlarm.getId();
    }


    @CacheEvict(value =  "configAlarmMapper.list",
            allEntries = true) // allEntries 清空所有缓存，因为此时不知道 id 对应的 permission 是多少。直接清理，简单有效
    @Override
    public void updateConfigAlarm(ConfigAlarmSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigAlarmExists(updateReqVO.getId());
        // 更新
        ConfigAlarmDO updateObj = BeanUtils.toBean(updateReqVO, ConfigAlarmDO.class);
        configAlarmMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigAlarm(Long id) {
        // 校验存在
        validateConfigAlarmExists(id);
        // 删除
        configAlarmMapper.deleteById(id);
    }

    @Override
        public void deleteConfigAlarmListByIds(List<Long> ids) {
        // 校验存在
        validateConfigAlarmExists(ids);
        // 删除
        configAlarmMapper.deleteByIds(ids);
        }

    private void validateConfigAlarmExists(List<Long> ids) {
        List<ConfigAlarmDO> list = configAlarmMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_ALARM_NOT_EXISTS);
        }
    }

    private void validateConfigAlarmExists(Long id) {
        if (configAlarmMapper.selectById(id) == null) {
            throw exception(CONFIG_ALARM_NOT_EXISTS);
        }
    }

    @Override
    public ConfigAlarmDO getConfigAlarm(Long id) {
        return configAlarmMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigAlarmDO> getConfigAlarmPage(ConfigAlarmPageReqVO pageReqVO) {
        return configAlarmMapper.selectPage(pageReqVO);
    }


    @Cacheable(value = "configAlarmMapper.list", key = "#configAlarmDO")
    @Override
    public List<ConfigAlarmDO> list(ConfigAlarmDO configAlarmDO) {
        return configAlarmMapper.selectList();
    }

}